package mobi.hami.huapin.service.impl;

import mobi.hami.huapin.base.Result;
import mobi.hami.huapin.dao.UserInfoMapper;
import mobi.hami.huapin.model.UserInfo;
import mobi.hami.huapin.service.UserInfoService;
import mobi.hami.huapin.util.Results;
import mobi.hami.huapin.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * Create by 小峰 on 2018/8/24.
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    private static final Logger logger = LoggerFactory.getLogger(UserInfoServiceImpl.class);

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public int save(UserInfo userInfo) {
        return  userInfoMapper.insertSelective(userInfo);
    }

    @Override
    public int update(UserInfo userInfo) {
        return userInfoMapper.updateByPrimaryKeySelective(userInfo);
    }

    @Override
    public UserInfo selectOnebyId(Integer id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public Result updatePhone(Integer id, String phone){
        String userId = userInfoMapper.selectOneUserIdBy(phone);
        if(StringUtil.isNotEmpty(userId)){
            return Results.failureWithStatus(1,"手机号已被占用");
        }
        int i = userInfoMapper.updatePhone(id,phone);
        if(i > 0){
            return Results.successWithStatus(0,"操作成功");
        }
        return Results.failureWithStatus(1,"操作失败");
    }
    public  void saveUserInfo(UserInfo userInfo){
        userInfoMapper.saveUserInfo(userInfo);
    }
}
